/*
 * Copyright 2010 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
package org.nicocube.wol.game.client;

import org.nicocube.gwt.util.client.ui.login.LoginFailedException;
import org.nicocube.gwt.util.client.ui.login.LoginHandler;
import org.nicocube.gwt.util.client.ui.login.LoginPanel;
import org.nicocube.wol.data.client.User;
import org.nicocube.wol.game.client.i18n.I18nCommon;
import org.nicocube.wol.game.client.sub.SubscribePanel;
import org.nicocube.wol.game.client.ui.AppearancePanel;
import org.nicocube.wol.game.client.ui.CommonRunAsyncCallback;
import org.nicocube.wol.game.client.ui.MainPanel;
import org.nicocube.wol.resource.client.WolBundle;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class Wol implements EntryPoint {

	private final LogSubServiceAsync logSubService = GWT
			.create(LogSubService.class);
	
	I18nCommon i18n = GWT.create(I18nCommon.class);
	WolBundle bundle = GWT.create(WolBundle.class);
	
	public void onModuleLoad() {
	    History.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				event.getValue();
				
			}
		});
		
		final LoginPanel loginPanel = new LoginPanel();
		RootPanel.get().add(loginPanel);

		final SimplePanel simplePanel = new SimplePanel();
		simplePanel.setStyleName(bundle.css().wolSublinkPanel());
		final Label sublink = new Label(i18n.showSubcribePanel());
		sublink.setStyleName(bundle.css().wolSublinkLink());
		simplePanel.add(sublink);
		RootPanel.get().add(simplePanel);
		
		final SubscribePanel subscribePanel = new SubscribePanel(logSubService) {
			@Override
			public void onSubscribe() {
				AppearancePanel apPanel = new AppearancePanel();
				RootPanel.get().remove(loginPanel);
				RootPanel.get().remove(simplePanel);
				
				RootPanel.get().add(apPanel);
			}
		};
		
		subscribePanel.setVisible(false);
		RootPanel.get().add(subscribePanel);

		sublink.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				loginPanel.setVisible(false);
				sublink.setVisible(false);
				subscribePanel.setVisible(true);
			}
		});
		
		loginPanel.addLoginHandler(new LoginHandler() {
			@Override
			public void onLogin(String login, String password) {

				logSubService.login(login, password, new AsyncCallback<User>() {
					@Override
					public void onSuccess(final User result) {
						RootPanel.get().remove(loginPanel);
						RootPanel.get().remove(simplePanel);

						GWT.runAsync(new CommonRunAsyncCallback("MainPanel") {
							@Override
							public void onSuccess() {
								MainPanel main = new MainPanel();
								RootPanel.get().add(main);
							}
						});
					}
					@Override
					public void onFailure(Throwable caught) {
						if (caught instanceof LoginFailedException) {
							loginPanel.displayFailureMessage();
						}
					}					
				});
			}
		});
	}
}
